package com.czy.owner.ui.store;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.BulletinListAdapter;
import com.czy.owner.api.BulletinListApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.BulletinModel;
import com.czy.owner.entity.WorkOrderModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.IntentUtils;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BulletinListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    private BulletinListAdapter adapter;
    private int page = 1;
    private Handler handler = new Handler();
    private String session = "";
    private String storeId;
//    private List<BulletinModel.ListBean> dataList = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_bulletin_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        toolbarTitle.setText("门店公告");
        session = UserHelper.getInstance().getSessionInfoModel(this).getSession();
        storeId = SharedPreferencesUtils.getParam(this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "";
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapterWithProgress(adapter = new BulletinListAdapter(this));
//        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, R.drawable.shape_order_fragment_recyclerview_divider_decoration));
        View view = LayoutInflater.from(this).inflate(R.layout.order_empty_view, (ViewGroup) this.getWindow().getDecorView(), false);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText("您的门店还没有相关公告");
        recyclerView.setEmptyView(view);
        recyclerView.setErrorView(R.layout.view_error_refresh);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
//        adapter.setError(R.layout.view_error_click).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                adapter.resumeMore();
//            }
//        });

        recyclerView.setRefreshListener(this);
        onRefresh();
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<BulletinModel.ListBean>() {
            @Override
            public void onItemClick(View view, int position, BulletinModel.ListBean model) {
                IntentUtils.startBulletin(BulletinListActivity.this, model.getInfoId() + "");
            }
        });

    }

    @Override
    protected void updateViews() {

    }

    public void getDataList(final boolean isfirst) {

        BulletinListApi bulletinListApi = new BulletinListApi(new HttpOnNextListener<BulletinModel>() {
            @Override
            public void onNext(final BulletinModel result) {
                if (result.getList() != null) {
                    if (isfirst) {
                        adapter.clear();

                    }
                    if (result.getList().size() < 9) {
                        adapter.stopMore();

                    }
                    adapter.addAll(result.getList());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, this);
        bulletinListApi.setSession(session);
        bulletinListApi.setStoreId(storeId);

        bulletinListApi.setPage(page + "");
        bulletinListApi.setSize("10");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(bulletinListApi);


    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(BulletinListActivity.this)) {
                    adapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page++;
                getDataList(false);

            }
        }, 1000);
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(BulletinListActivity.this)) {
                    adapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page = 1;
                getDataList(true);
            }
        }, 1000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
